<?php
 
class Notifications extends MY_Controller 
{
	var $curl = 'notifications';
	var $name = 'Notifications';
	var $search = '';
	var $language;
	var $submenu = "_menu";
	var $id_category;
	var $category_alias;

	function __construct()
	{
		parent::__construct();
        
        $this->load->model('/public/notification_model');
        
		$this->validation->set_fields($this->notification_model->fields);

		$this->data['Title'] = T_('Notification Manager');

		$this->language = parent::getLang();
		
		$this->name = T_('Notification Manager');		
    }
	
	function index()
	{		
		$config = array('per_page' => 10);
		$this->notification_model->search($config);
		//$this->search = $this->data['search'];
		
		$this->renderLayout();
    }

	function create()
	{
        if (count($_POST))
		{
			$rules = array('title', 'message', 'status');
			$this->validation->set_rules($this->notification_model->getRules($rules));

			if ($this->validation->run() === TRUE)
			{							
				$item['title'] 		= $this->validation->title;
				$item['message'] 	= $this->validation->message;
				$item['status'] 	= $this->validation->status;			
				
				if ($this->notification_model->insert($item))
				{
					redirect($this->curl . '/index/'. $this->category_alias);	
				}				
			}
		}

		$this->data['Title'] .= ' / '. T_('Создать');
		$this->data['wysiwyg'] = true;
		
		$this->renderLayout();        
    }
    
	function edit($id)
	{	
		if (!empty($id))
		{
			$object = $this->notification_model->get($id);
			
			if (!count($object))
			{
				$this->data['view'] = '_elements/query_no_object';
				$this->load->view($this->data['layout'], $this->data);
				return false; 
			}
		
			if (!count($_POST))
			{
				$this->validation->id 		= $object['id']; 
				$this->validation->title 	= $object['title'];
				$this->validation->message 	= $object['message'];	
	        	$this->validation->status 	= $object['status'];	
			}
			else
			{
				$rules = array('title', 'message', 'status');
				$this->validation->set_rules($this->notification_model->getRules($rules));
	
				if ($this->validation->run() === TRUE)
				{
					$item['id'] 			= $this->validation->id;
					$item['title'] 			= $this->validation->title;
					$item['message'] 		= $this->validation->message;	
					$item['status'] 		= $this->validation->status;												
					$item['date_modified'] 	= date('Y-m-d H:i:s');
					
					$this->notification_model->update($item);					
					
					redirect($this->curl . '/index/');				
				}
			}			
		}
           
		$this->data['Title'] .= ' / ' . $object['title'] . ' / '. T_('Edit');
		$this->data['wysiwyg'] = true; 
		
		$this->renderLayout();
    }
        
	function delete($id)
	{
		$data = $this->notification_model->delete($id);

		if ($data)
		{
			redirect($this->curl . '/index');	
		}
		else
		{
			redirect($this->curl . '/edit/'. $id);
		}
	}
	        
	function deleteItems()
	{
		$data = $this->notification_model->delete($_REQUEST['items']);

		echo $data;
	}
	    
	function activity()
	{
		$data = $this->utils->publishItems($this->article_model->table, $this->article_model->prKey, $this->article_model->publishedField, $_REQUEST['items'], $_REQUEST['action']);
		echo $this->utils->php2js($data);
	}
	
	/*
	 * Static Pages 
	*/
	function view($id)
	{
		$this->data['Title'] = '';
		
		$this->data['notification'] = $this->notification_model->get($id);
		
		if (!empty($this->data['notification']))
		{
			$this->data['Title'] = $this->data['notification']->title;
		}
		
		$this->view = '/notifications/view';
		
		$this->renderLayout();	
	}
}

?>